package com.lc202310;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @Create 2023/10/13 23:17
 * @Version 1.0
 */
public class Solution17 {
    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits.length() == 0) {
            return res;
        }
        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        addChar(res, map, 0, digits, new StringBuffer());
        return res;
    }

    private static void addChar(List<String> res, Map<Character, String> map, int index, String digits, StringBuffer str) {
        if (digits.length() == index) {
            res.add(str.toString());
            return;
        }
        String chars = map.get(digits.charAt(index));
        for (int i = 0; i < chars.length(); i++) {
            str.append(chars.charAt(i));
            addChar(res, map, index + 1, digits, str);
            // 执行完后，删除给之后的程序用
            str.deleteCharAt(index);
        }
    }



    public static void main(String[] args) {
        letterCombinations("234");
    }
}
